package test;

/**
 * @Author Stringzhua
 * @Date 2024/11/8 19:07
 * description:
 */
public class test02 {
    public static void main(String[] args) {
//        String s = "abbcbba";
        String s = "abbcbbaaaa";
        System.out.println(check(s));
    }

    public static String check(String s) {
        //字符数组
        char[] chars = s.toCharArray();
        int maxlength = 0;//计数器
        int index = 0;//起始下标
        for (int i = 0; i < chars.length; i++) {//起始下标i
            //结束下标j
            for (int j = i; j < chars.length; j++) {
                boolean flag = true;//假设是回文串
                //计算当前的串长度abbcbba  7
                int longs = j - i + 1;
                for (int k = 0; k < longs; k++) {
                    //遍历当前串是不是回文串
                    if (chars[i + k] != chars[j - k]) {
                        //如果有字符不相等,说明不是回文串
                        flag = false;
                        break;
                    }
                }
                //如果长度大于之前计数器的值并且是回文串则赋值
                if (longs > maxlength && flag) {
                    //回文串长度
                    maxlength = longs;
                    //起始下标
                    index = i;
                }
            }
        }
        //由找到的最长的回文串的起始下标index和长度length,截取
        //原字符串返回截取的字符串
        return s.substring(index, index + maxlength);
    }
}